#!/bin/sh
#desc:Manage rules
#type:local
#package:odai_rules

RULESDIR="/etc/opendomo/rules"

if test -d /etc/opendomo/sequences; then
	COMMANDS="$COMMANDS,@seq:Sequences"
	cd /etc/opendomo/sequences
	for i in *; do
		if test "$i" != "*"; then
			desc=`grep '#desc' $i | cut -f2 -d:`
			if test -z "$desc"; then
				desc="$i"
			fi
			COMMANDS="$COMMANDS,$i:$desc"
		fi
	done
fi

#COMMANDS="$COMMANDS,@scripts:Scripts"
#cd /usr/local/opendomo/
#for i in *.sh; do
#	DESC=`grep '#desc' $i | cut -f2 -d:`
#	if test -z "$DESC"; then
#		DESC="$i"
#	fi
#	COMMANDS="$COMMANDS,$i:$DESC"
#done


# If this directory does not exist, we create it with a new rule as example
if ! test -d $RULESDIR; then
	mkdir -p $RULESDIR
	echo '#!/bin/sh -e
#desc:This rule updates the package lists at 23:00
#command:updatePackageList.sh  
test $(hour) = "23:00"
'> $RULESDIR/updatefiles.rule
	chown admin:admin -R $RULESDIR 2>/dev/null
	chmod +x $RULESDIR/*.rule
fi

# A rule is a condition (or a set of conditions) and a pair of 
# actions

if test -z "$1";then
	cd $RULESDIR
	echo "#> Available"
	echo "list:`basename $0`	selectable"
	EXISTS=0;
	for r in *; do
		if test "$r" != "*"; then
			DESC=`grep '#desc' $r | cut -f2 -d: `
			echo "	-$r	$DESC	rule"
			EXISTS=1
		fi
	done

	if [ $EXISTS == 0 ]; then
		echo "# There are no rules. Please, go to Add."
	fi

	echo "actions:"
	echo "	addRule.sh	Add"
	echo "	delRule.sh	Delete"
	echo 
else
	if test "$2" = "savedesc" && test -z "$3" ; then
		DESC=`echo $1 | sed 's/+/ /g'`
		code=`echo $1 | tr A-Z a-z | sed 's/[^a-z0-9]//g'`
		FILE="$code.rule"
	else	
		FILE="$1"
	fi
	#Some rule is selected
	if ! test -f $RULESDIR/$FILE; then
		echo "#INFO Creating new rule"
		echo "#INFO Please, go to Requirements"
		if test "$2" = "savedesc"; then
			echo "#!/bin/sh -e" > $RULESDIR/$FILE
		else
			exit
		fi
	fi
	case $2 in
		"addcond")
			echo "test \$($3) $4 \"$5\"" >> $RULESDIR/$FILE
			echo "#INFO: Requirement added"
			/usr/local/opendomo/manageRules.sh $FILE
			exit 0

		;;
		"savedesc")
			if test -z "$3"; then 
				DESC="`echo $1 | sed 's/+/ /g'`"
			else
				DESC="`echo $3 | sed 's/+/ /g'`"
			fi
			sed -i '/#desc/ d' $RULESDIR/$FILE 
			echo "#desc:$DESC" >> $RULESDIR/$FILE
		;;
		"setifrule")
			sed -i '/#command/ d' $RULESDIR/$FILE
			echo "#command:$3 $4" >> $RULESDIR/$FILE
			IFACT="$3"
			IFPARAM="$4"
		;;
#		"setelserule")
#			ELACT="$3"
#			ELPARAM="$4"
#		;;
	esac

	DESC=`grep '#desc' $RULESDIR/$FILE | cut -f2 -d:`
	echo "#> General data"
	echo "form:`basename $0`"
	echo "	rule	rule	hidden	$FILE"
	echo "	action	action	hidden	savedesc"
	echo "	name	Name	text	$DESC"
	echo "actions:"
	echo "	`basename $0`	Save"
	echo "	addRuleRequirement.sh	Add requirement"
	echo

	echo "#> Requirements"
	echo "list:`basename $0`	detailed"
	for line in `grep -n test $RULESDIR/$FILE | sed 's/ /:/g'`; do
		lineno=`echo $line | cut -f1 -d:`
		var=`echo $line | cut -f3 -d: | sed 's/[^a-z0-9.]//g'`
		op=`echo $line | cut -f4 -d:`
		value=`echo $line | cut -f5- -d: | sed 's/"//g'`
		case $op in
			"=")
				op="is equal to"
			;;
			"!=")
				op="is different than"
			;;
			"-gt")
				op="is greater than"
			;;
			"-lt")
				op="is smaller than"
			;;
		esac
		if test "$var" = "weekday"; then
			case "$value" in
				"1")
					value="monday"
				;;
				"2")
					value="tuesday"
				;;
				"3")
					value="wednesday"
				;;
				"4")
					value="thursday"
				;;
				"5")
					value="friday"
				;;
				"6")
					value="saturday"
				;;
				"7")
					value="sunday"
				;;
			esac
		fi
		echo "	$FILE-$lineno	$var	condition	$op [$value]"
	done 
	echo "actions:"
	echo "	delRuleCondition.sh	Delete requirements"
	echo

	IFACT=`grep '#command' $RULESDIR/$FILE | cut -f2 -d: | cut -f1 -d' '`
	IFPARAM=`grep '#command' $RULESDIR/$FILE | cut -f2 -d' '`
	echo "#> If all requirements are acomplished"
	echo "form:`basename $0`"

	if test -z "$COMMANDS"; then
		echo "# There are no sequences, please go to manage sequences."
		echo "actions:"
		echo "	manageSequence.sh	Manage sequences"
	else
		echo "	rule	rule	hidden	$FILE"
		echo "	action	action	hidden	setifrule"
		echo "	command	Execute action or sequence	list[$COMMANDS]	$IFACT"
		echo "	params	Additional parameters (optional)	text	$IFPARAM"
		echo "actions:"
		echo "	`basename $0`	Save"
	fi

	echo
fi
echo
